package com.hy.z.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hy.z.utils.TreeNode;
import com.hy.z.dto.UserRoleDto;
import com.hy.z.entity.Role;
import com.hy.z.entity.UserRole;
import com.hy.z.result.Result;
import com.hy.z.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Fhy
 * @since 2021-01-14
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;


    @RequestMapping("/queryAllRoles")
    public Result queryAllRoles(long pageNum, long pageSize) {
        IPage<Role> roleIPage = roleService.queryAllRole(pageNum, pageSize);
        return Result.success(roleIPage);
    }

    /**
     * 查询选角色已有的资源id
     */
    @RequestMapping("/queryRoleIds")
    public Result queryRoleIds(String roleId) {
        List<TreeNode> treeNodes = roleService.queryRoleIds(roleId);
        return Result.success(treeNodes);
    }

    /**
     * 前段下拉角色列表
     */
    @RequestMapping("/queryRoleList")
    public Result queryRoleList() {
        List<Role> roles = roleService.queryRoleList();
        return Result.success(roles);
    }

    /**
     * 分配角色
     */
    @RequestMapping("/updateRoleByUserId")
    public Result updateRoleByUserId(UserRoleDto userRoleDto) {
        System.out.println("hello");
        boolean b = roleService.updateRoleByUserId(userRoleDto);
        if (b){
            return Result.success();
        }
        return Result.error();
    }

    /**
     * 管理员修改角色权限
     *
     * @param roleId
     * @param resourcesIds
     * @return
     */
    @RequestMapping("/updatePermission")
    public Result updatePermission(String roleId, String resourcesIds) throws Exception {
        //总管理员不能被分配角色
        if (roleId.equals("1")){
            return Result.success();
        }
        boolean b = roleService.updatePermission(roleId, resourcesIds);
        if (b) {
            return Result.success();
        }
        return Result.error();
    }


}

